package com.gopay.common.rcs.dao.translimit.impl;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cipher.utils.SensitiveInfoUtils;
import com.gopay.common.constants.rcs.TranDirection;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.acps.AcctProd;
import com.gopay.common.domain.rcs.RcsBAcctLimitStatistics;
import com.gopay.common.rcs.dao.translimit.RcsBAcctLimitStatisticsDAO;
import com.gopay.common.util.DateUtils;

/**
 * 额度统计数据库访问对象 
 * @RcsTransLimitStatisticsDAO.java
 * @author liujy
 * @2013-7-23 下午6:35:41  www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("rcsBAcctLimitStatistics")
public class RcsBAcctLimitStatisticsDAOImpl extends BaseDAOImpl<RcsBAcctLimitStatistics, Serializable> implements RcsBAcctLimitStatisticsDAO{
    
    @Override
    public RcsBAcctLimitStatistics findTransLimitStatistics(String custId, AcctProd acctProd, IntTxnCd intTxnCd,
                                                            TranDirection tranDirection, String pbocBankCode, String bankAcctNum, String tranDate){
        StringBuilder hql = new StringBuilder("from RcsBAcctLimitStatistics a where ");
        hql.append("a.custId=:custId");
        hql.append(" and a.prodCode=:prodCode");
        hql.append(" and a.gopayIntTxnCd=:intTxnCd");
        hql.append(" and a.tranDirection=:tranDirection");
        hql.append(" and a.statcsDate=:tranDate");
        //delete by zyt，2017-2-23 在后续使用in查询
        //hql.append(" and a.bankAcctNum=:bankAcctNum");
       
        
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("custId", custId);
        param.put("prodCode", acctProd.toString());
        param.put("intTxnCd", intTxnCd.value);
        param.put("tranDirection", tranDirection.value);
        param.put("tranDate", tranDate);
        /**
         * modified by zyt,敏感信息历史密钥加密，使用in查询；
         * 2017-2-22
         */
        //start++++++++++++
        List<String> hisNoList = null;
        if(StringUtils.isNotBlank(bankAcctNum)){
        	hisNoList = SensitiveInfoUtils.getAllHisCipherText(bankAcctNum);
        	hql.append(" and a.bankAcctNum in (:hisNoList) ");
        	param.put("hisNoList",  hisNoList);
        }
        //end+++++++++
        if(StringUtils.isNotBlank(pbocBankCode)){
            hql.append(" and a.pbocBankCode=:pbocBankCode");
            param.put("pbocBankCode", pbocBankCode);
        }

        List<RcsBAcctLimitStatistics> list = super.findByHql(hql.toString(), param);
        if(null == list || list.isEmpty()){
            return null;
        }
        
        return list.get(0);
    }

    @Override
    public RcsBAcctLimitStatistics add(RcsBAcctLimitStatistics statistics) {
        statistics.setCreatedTs(DateUtils.getSystemDate());
        statistics.setDayLimitStatcs(new BigDecimal("0.00"));
        statistics.setDayTimesStatcs(0);
        statistics.setLastUpdTrancode(statistics.getGopayIntTxnCd());
        statistics.setLastUpdTs(DateUtils.getSystemDate());
        statistics.setLastUpdUserId("");
        return save(statistics);
    }
}
